package com.ytkj.tradeserver.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ytkj.tradeserver.config.StateCode;
import com.ytkj.tradeserver.dao.ProductInfoMapper;
import com.ytkj.tradeserver.entity.ProductInfo;
import com.ytkj.tradeserver.model.appCom.ResultMessageModel;
import com.ytkj.tradeserver.service.IProductService;

@Service("productInfoServiceImpl")
public class ProductInfoServiceImpl implements IProductService {

	@Resource
	private ProductInfoMapper productInfoMapper;

	@Override
	public String getProductInfoList() {

		List<ProductInfo> productList = null;
		String jsonData = "";
		productList = productInfoMapper.queryAllDepartment();

		if (null != productList) {

			jsonData = JSON.toJSONString(productList);
		}

		return jsonData;
	}

	@Override
	public String insertProductInfo(ProductInfo productInfo) {
		// TODO Auto-generated method stub
		String messageResult = "";
		ResultMessageModel resultModel = null;
		
		messageResult = getProductInfo(productInfo.getPid());
		
		if ("".equals(messageResult)) {
			
			int insertFlag = productInfoMapper.insert(productInfo);

			if (insertFlag > 0) {
				resultModel = ResultMessageModel.toResultMessageModel(StateCode.API_PRODUCT_SUCCESS.getCode(), StateCode.API_PRODUCT_SUCCESS.getMsg());
			}else {
				resultModel = ResultMessageModel.toResultMessageModel(StateCode.API_PRODUCT_FAILE.getCode(), StateCode.API_PRODUCT_FAILE.getMsg());
			}
		}else {
			
			resultModel = ResultMessageModel.toResultMessageModel(StateCode.API_PRODUCT_NORE_FAILE.getCode(), StateCode.API_PRODUCT_NORE_FAILE.getMsg());
		}
		
		messageResult = JSON.toJSONString(resultModel);
		return messageResult;
	}

	@Override
	public String deleteProductInfo(String pid) {
		// TODO Auto-generated method stub
		String messageResult = "";
		ResultMessageModel resultModel = null;
		int productFlag = productInfoMapper.deleteProductInfoWithPid(pid);
		
		if (productFlag > 0) {
			resultModel = ResultMessageModel.toResultMessageModel(StateCode.API_PRODUCT_DEL_SUCCESS.getCode(), StateCode.API_PRODUCT_DEL_SUCCESS.getMsg());
		}else {
			resultModel = ResultMessageModel.toResultMessageModel(StateCode.API_PRODUCT_DEL_FAILE.getCode(), StateCode.API_PRODUCT_DEL_FAILE.getMsg());
		}
		
		messageResult = JSON.toJSONString(resultModel);
		return messageResult;
	}

	@Override
	public String getProductInfo(String pid) {
		// TODO Auto-generated method stub
		String  resultData = "";
	 
		
		ProductInfo  pInfo = productInfoMapper.queryProductInfoWithPid(pid);
		
		System.out.println("===========inifo======"+JSON.toJSONString(pInfo));
		
		if (null != pInfo) {
			
			resultData = JSON.toJSONString(pInfo);
		}
		
		return resultData;
	}

}
